package com.jbinfo.qingcaowan.user.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.bana.common.util.basic.StringUtils;
import org.bana.common.util.page.PageCond;
import org.bana.common.util.page.PageCondUtil;
import org.bana.common.util.page.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jbinfo.qingcaowan.base.dao.BaseTimeDao;
import com.jbinfo.qingcaowan.base.dto.BaseTimeDTO;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.exam.dao.ExamScoreDao;
import com.jbinfo.qingcaowan.exam.dto.ExamScoreDTO;
import com.jbinfo.qingcaowan.security.entity.Org;
import com.jbinfo.qingcaowan.security.entity.OrgSub;
import com.jbinfo.qingcaowan.security.entity.Position;
import com.jbinfo.qingcaowan.security.entity.User;
import com.jbinfo.qingcaowan.user.dao.OrgDao;
import com.jbinfo.qingcaowan.user.dao.OrgSubDao;
import com.jbinfo.qingcaowan.user.dao.PositionDao;
import com.jbinfo.qingcaowan.user.dao.ScoreDetailDAO;
import com.jbinfo.qingcaowan.user.dao.UserDao;
import com.jbinfo.qingcaowan.user.dto.ScoreDetailDTO;
import com.jbinfo.qingcaowan.user.dto.ScoreQueryDTO;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.wx.WxInfo;
import com.jbinfo.qingcaowan.wx.WxLogin;
import com.jbinfo.qingcaowan.wx.WxUserInfo;

/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO 用户管理service实现类
* @author ZhangLei 
* @date 2016年6月22日 上午10:23:02
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	
	private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="orgDao")
	private OrgDao orgDao;
	
	@Resource(name="orgSubDao")
	private OrgSubDao orgSubDao;
	
	@Resource(name="positionDao")
	private PositionDao positionDao;
	
	@Resource(name="baseTimeDao")
	private BaseTimeDao baseTimeDao;
	
	@Resource
	private OrgService orgService;
	
	@Resource(name="scoreDetailDAO")
	private ScoreDetailDAO scoreDetailDAO;
	
	@Resource(name="examScoreDao")
	private ExamScoreDao examScoreDao;
	
	/**
	 * 
	 * @Description: TODO 根据code获取用户
	 * @date 2016年7月8日 下午7:45:08 
	 * @param userCode
	 * @return 
	 * @see com.jbinfo.partybuilding.security.BaseSignService.UserService#getByCode(java.lang.String)
	 */
	@Override
	public User getByCode(String userCode) {
		return userDao.getByCode(userCode);
	}
	
	// 根据用户名、密码进行验证
	@Override
	public Object checkUserAndPwd(String username, String password) {

		//JSONResult jsonResult = new JSONResult();
		//1、验证传入参数
		if(StringUtils.isBlank(username)){
			throw new BusinessException(BusinessExceptionCode.login_null_username);
		}
//		if(StringUtils.isBlank(password)){
//			throw new BusinessException(BusinessExceptionCode.login_null_password);
//		}
		
		// 根据用户名验证是否存在
		UserDTO userDTO = new UserDTO();
		
		User user = userDao.getByCode(username);
		if (user == null) {
			throw new BusinessException(BusinessExceptionCode.login_invalid_username);
		}else {
			// 对传入的密码进行MD5加密(shiro 工具)
//			String encryptPwd = new SimpleHash("md5", password, ByteSource.Util.bytes("salt-code"), 2).toHex();
			
			// 如果与表里密码相符，则通过验证；否则，不通过
//			if (!user.getUserPwd().equals(encryptPwd)) {
//				throw new BusinessException(BusinessExceptionCode.login_invalid_password);
//			}
			
			userDTO = ClonePojoUtil.copyClassFromTo(user, UserDTO.class);
			
		}
		// 获取平台名称
		if (user.getUserOrg() != null && !user.getUserOrg().isEmpty()) {
			Org org = orgDao.selectByPrimaryKey(user.getUserOrg());
			userDTO.setOrgName(org.getOrgName());
		}
		
		// 获取组织名称
		if (user.getUserSubOrg() != null && !user.getUserSubOrg().isEmpty()) {
			OrgSub orgSub = orgSubDao.getById(user.getUserSubOrg());
			userDTO.setOrgSubName(orgSub.getSubOrgName());
		}
		
		// 获取职务名称
		if (user.getUserPosition() != null && !user.getUserPosition().isEmpty()) {
			Position position = positionDao.getById(user.getUserPosition());
			userDTO.setPositionName(position.getPositionName());
		}
		
		/*jsonResult.setResultCode("200");
		jsonResult.setResultBean(userDTO);*/
				
		return userDTO;
	}

	// 根据用户Id查询用户信息，包含平台信息、组织信息、职务信息
	@Override
	public Object getUserInfoById(UserDTO userDTO) {
		if (userDTO.getUserId() == null || userDTO.getUserId().equals("")) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户Id不能为空!");
		}
		
		User user = ClonePojoUtil.copyClassFromTo(userDTO, User.class);
		
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		return userRet;
	}

	// 更新用户信息
	@Override
	public Object updateUserInfoById(UserDTO userDTO) {
		// TODO Auto-generated method stub
		if (userDTO.getUserId() == null || userDTO.getUserId().equals("")) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户Id不能为空!");
		}
		
		User user = ClonePojoUtil.copyClassFromTo(userDTO, User.class);
		
		return userDao.updateByPrimaryKeySelective(user);
	}

	// 获取“我的排名”中前几名
	@Override
	public Object findTopRank(BaseTimeDTO baseTimeDTO) {
		
		// 根据orgId 查询所有parentOrgId
		/*JSONResult pOrgJson = (JSONResult) orgService.selectParentOrgId(baseTimeDTO.getOrgId());
		String pOrgId = pOrgJson.getResultBean();
		
		String [] orgIds = pOrgId.split(",");*/
		
		List<String> orgIdList = new ArrayList<String>();
		
		/*for (int i = 0; i < orgIds.length; i++) {
			String orgId = orgIds[i];
			orgIdList.add(orgId);
		}*/
		orgIdList.add(baseTimeDTO.getOrgId());
		
//		DataUtil.removeDup(orgIdList);
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<BaseTimeDTO> dtoList = baseTimeDao.selectTopUser(orgIdList, null, rankNum);
		
		List<BaseTimeDTO> baseTimeList = new ArrayList<BaseTimeDTO>();
		
		for (BaseTimeDTO dto: dtoList) {
//			BaseTimeDTO baseTime = ClonePojoUtil.copyClassFromTo(dto, BaseTimeDTO.class);
			double dTotalTime = dto.getTotalTime();
			dTotalTime = Math.ceil(dTotalTime);// 向上取整
			int nTime = (int)dTotalTime;
			Integer nTotalTime = Integer.valueOf(nTime);
			dto.setTotalReadTime(nTotalTime);
			
			baseTimeList.add(dto);
		}
		
		return baseTimeList;
	}

	// “我的排名”中登陆用户的排名
	@Override
	public Object findeMyRank(BaseTimeDTO baseTimeDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		// 根据orgId 查询所有parentOrgId
//		JSONResult pOrgJson = (JSONResult) orgService.selectParentOrgId(baseTimeDTO.getOrgId());
//		String pOrgId = pOrgJson.getResultBean();
//		
//		String [] orgIds = pOrgId.split(",");
		
		List<String> orgIdList = new ArrayList<String>();
		
//		for (int i = 0; i < orgIds.length; i++) {
//			String orgId = orgIds[i];
//			orgIdList.add(orgId);
//		}
		orgIdList.add(baseTimeDTO.getOrgId());
		// 去重
//		DataUtil.removeDup(orgIdList);
		
		List<BaseTimeDTO> dtoList = baseTimeDao.selectTopUser(orgIdList, null, null);
		
		for (BaseTimeDTO baseTime: dtoList) {
			nRank ++;
			if (baseTime.getUserId().equals(baseTimeDTO.getUserId())) {
				break;
			}
		}
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		nMyRank = Integer.valueOf(nRank);
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“我的排名”中当前组织下所有用户排名及登陆用户的排名
	@Override
	public Object findTopAndMyRank(BaseTimeDTO baseTimeDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		baseTimeDTO.setOrgId(userDTO.getUserOrg());
		
		//获取“我的排名”中当前组织下所有用户排名
		List<BaseTimeDTO> baseTimeDTOList = (List<BaseTimeDTO>) findTopRank(baseTimeDTO);
		
		// 获取“我的排名”中登陆用户的排名
		dto = (UserDTO) findeMyRank(baseTimeDTO);
		dto.setBaseTimeList(baseTimeDTOList);
		
		return dto;
	}
	
	// 获取“支部排名”中支部中阅读时间排名中前几名
	@Override
	public Object findTopRankInSubOrg(BaseTimeDTO baseTimeDTO) {
		
		String subOrgId = baseTimeDTO.getSubOrgId();
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<BaseTimeDTO> dtoList = baseTimeDao.selectTopUser(null, subOrgId, rankNum);
		
		List<BaseTimeDTO> baseTimeList = new ArrayList<BaseTimeDTO>();
		
		for (BaseTimeDTO dto: dtoList) {
//			BaseTimeDTO baseTime = ClonePojoUtil.copyClassFromTo(dto, BaseTimeDTO.class);
			double dTotalTime = dto.getTotalTime();
			dTotalTime = Math.ceil(dTotalTime);// 向上取整
			int nTime = (int)dTotalTime;
			Integer nTotalTime = Integer.valueOf(nTime);
			dto.setTotalReadTime(nTotalTime);
			
			baseTimeList.add(dto);
		}
		
		return baseTimeList;
	}

	// “支部排名”中登陆用户在支部中的排名
	@Override
	public Object findeMyRankInSubOrg(BaseTimeDTO baseTimeDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		String subOrgId = baseTimeDTO.getSubOrgId();
		
		List<BaseTimeDTO> dtoList = baseTimeDao.selectTopUser(null, subOrgId, null);
		
		for (BaseTimeDTO baseTime: dtoList) {
			nRank ++;
			if (baseTime.getUserId().equals(baseTimeDTO.getUserId())) {
				nMyRank = Integer.valueOf(nRank);
				break;
			}
		}
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“我的排名”中当前支部下所有用户排名及登陆用户的排名
	@Override
	public Object findTopAndMyRankInSubOrg(BaseTimeDTO baseTimeDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		baseTimeDTO.setOrgId(userDTO.getUserOrg());
		baseTimeDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“我的排名”中当前组织下所有用户排名
		List<BaseTimeDTO> baseTimeDTOList = (List<BaseTimeDTO>) findTopRankInSubOrg(baseTimeDTO);
		
		// 获取“我的排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankInSubOrg(baseTimeDTO);
		dto.setBaseTimeList(baseTimeDTOList);
		
		return dto;
	}
	
	// 获取“支部排名”中阅读时间前几名
	@Override
	public Object findTopOrgRankByRead(BaseTimeDTO baseTimeDTO) {
		List<String> orgIdList = new ArrayList<String>();
		orgIdList.add(baseTimeDTO.getOrgId());
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<BaseTimeDTO> dtoList = baseTimeDao.selectTopOrgByRead(null, rankNum);
		
		List<BaseTimeDTO> baseTimeList = new ArrayList<BaseTimeDTO>();
		
		for (BaseTimeDTO dto: dtoList) {
//			BaseTimeDTO baseTime = ClonePojoUtil.copyClassFromTo(dto, BaseTimeDTO.class);
			double dTotalTime = dto.getTotalOrgReadTime();
			dTotalTime = Math.ceil(dTotalTime);// 向上取整
			int nTime = (int)dTotalTime;
			Integer nTotalTime = Integer.valueOf(nTime);
			dto.setTotalOrgReadTime(nTotalTime);
			
			baseTimeList.add(dto);
		}
		
		return baseTimeList;
	}
	
	// 支部排名--阅读时间  前几名及当前支部排名
	@Override
	public Object findTopAndMyRankByOrgRead(BaseTimeDTO baseTimeDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
//		baseTimeDTO.setOrgId(userDTO.getUserOrg());
		baseTimeDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“支部排名”中当前组织下所有支部排名
		List<BaseTimeDTO> baseTimeDTOList = (List<BaseTimeDTO>) findTopOrgRankByRead(baseTimeDTO);
		
		// 获取“支部排名”中登陆用户所在支部的排名
		dto = (UserDTO) findeMyOrgRankByRead(baseTimeDTO);
		dto.setBaseTimeList(baseTimeDTOList);
		
		return dto;
	}
	
	// “支部排名”--阅读时间 中登陆用户所在支部的排名
	@Override
	public Object findeMyOrgRankByRead(BaseTimeDTO baseTimeDTO) {
		Integer nMyOrgRank = 0;
		int nRank = 0;
		
		String subOrgId = baseTimeDTO.getSubOrgId();
		
		List<BaseTimeDTO> dtoList = new ArrayList<BaseTimeDTO>();
		
//		if (subOrgId != null && !subOrgId.isEmpty()) {
			
			dtoList = baseTimeDao.selectTopOrgByRead(null, null);
//		}
		if (dtoList != null && !dtoList.isEmpty()) {
			for (BaseTimeDTO baseTime: dtoList) {
				nRank ++;
				if (baseTime.getSubOrgId() != null 
						&& !baseTime.getSubOrgId().isEmpty()) {
					
					if (baseTime.getSubOrgId().equals(subOrgId)) {
						nMyOrgRank = Integer.valueOf(nRank);
						break;
					}
				}
				
			}
		}
		
		
		User user = new User();
		user.setUserId(baseTimeDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyOrgRank(nMyOrgRank);
		return userRet;
	}
	
	// 获取“积分排名”中前几名
	@Override
	public Object findTopRankByScore(ScoreDetailDTO scoreDetailDTO) {
		
		// 根据orgId 查询所有parentOrgId
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(scoreDetailDTO.getOrgId());
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ScoreDetailDTO> dtoList = scoreDetailDAO.selectTopUserScore(orgIdList, null, rankNum);
		
		List<ScoreDetailDTO> scoreDetailList = new ArrayList<ScoreDetailDTO>();
		
		for (ScoreDetailDTO dto: dtoList) {
			
//			ScoreDetailDTO scoreDetail = ClonePojoUtil.copyClassFromTo(dto, ScoreDetailDTO.class);
			
			Integer nTotalSocre = dto.getTotalScore();
			dto.setTotalScore(nTotalSocre);
			
			scoreDetailList.add(dto);
		}
		
		return scoreDetailList;
	}
	
	// “积分排名”中登陆用户的排名
	@Override
	public Object findeMyRankByScore(ScoreDetailDTO scoreDetailDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(scoreDetailDTO.getOrgId());
		// 去重
//			DataUtil.removeDup(orgIdList);
		
		List<ScoreDetailDTO> dtoList = scoreDetailDAO.selectTopUserScore(orgIdList, null, null);
		
		for (ScoreDetailDTO scoreDetail: dtoList) {
			nRank ++;
			if (scoreDetail.getUserId().equals(scoreDetailDTO.getUserId())) {
				nMyRank = Integer.valueOf(nRank);
				break;
			}
		}
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“积分排名”中当前组织下所有用户排名及登陆用户的排名
	@Override
	public Object findTopAndMyRankByScore(ScoreDetailDTO scoreDetailDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		scoreDetailDTO.setOrgId(userDTO.getUserOrg());
		
		//获取“积分排名”中当前组织下所有用户排名
		List<ScoreDetailDTO> scoreDetailDTOList = (List<ScoreDetailDTO>) findTopRankByScore(scoreDetailDTO);
		
		// 获取“我的排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankByScore(scoreDetailDTO);
		dto.setScoreDetailList(scoreDetailDTOList);
		
		return dto;
	}
	
	// 获取“积分排名”中同一支部下前几名
	@Override
	public Object findTopRankByScoreInSubOrg(ScoreDetailDTO scoreDetailDTO) {
		
		String subOrgId = scoreDetailDTO.getSubOrgId();
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ScoreDetailDTO> dtoList = scoreDetailDAO.selectTopUserScore(null, subOrgId, rankNum);
		
		List<ScoreDetailDTO> scoreDetailList = new ArrayList<ScoreDetailDTO>();
		
		for (ScoreDetailDTO dto: dtoList) {
			
//			ScoreDetailDTO scoreDetail = ClonePojoUtil.copyClassFromTo(dto, ScoreDetailDTO.class);
			
			Integer nTotalSocre = dto.getTotalScore();
			dto.setTotalScore(nTotalSocre);
			
			scoreDetailList.add(dto);
		}
		
		return scoreDetailList;
	}
		
	// “积分排名”中登陆用户在支部中的排名
	@Override
	public Object findeMyRankByScoreInSubOrg(ScoreDetailDTO scoreDetailDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		String subOrgId = userRet.getUserSubOrg();
		
		List<ScoreDetailDTO> dtoList = scoreDetailDAO.selectTopUserScore(null, subOrgId, null);
		
		for (ScoreDetailDTO scoreDetail: dtoList) {
			nRank ++;
			if (scoreDetail.getUserId().equals(scoreDetailDTO.getUserId())) {
				nMyRank = Integer.valueOf(nRank);
				break;
			}
		}
		
		
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“积分排名”中当前组织下所有用户排名及登陆用户在支部中的排名
	@Override
	public Object findTopAndMyRankByScoreInSubOrg(ScoreDetailDTO scoreDetailDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
//		scoreDetailDTO.setOrgId(userDTO.getUserOrg());
		scoreDetailDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“积分排名”中当前组织下所有用户排名
		List<ScoreDetailDTO> scoreDetailDTOList = (List<ScoreDetailDTO>) findTopRankByScoreInSubOrg(scoreDetailDTO);
		
		// 获取“我的排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankByScoreInSubOrg(scoreDetailDTO);
		dto.setScoreDetailList(scoreDetailDTOList);
		
		return dto;
	}
	
	// 获取“支部排名”积分排名中前几名
	@Override
	public Object findTopRankByOrgScore(ScoreDetailDTO scoreDetailDTO) {
		
		// 根据orgId 查询所有parentOrgId
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(scoreDetailDTO.getOrgId());
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ScoreDetailDTO> dtoList = scoreDetailDAO.selectTopOrgScore(null, rankNum);
		
		List<ScoreDetailDTO> scoreDetailList = new ArrayList<ScoreDetailDTO>();
		
		for (ScoreDetailDTO dto: dtoList) {
			
//			ScoreDetailDTO scoreDetail = ClonePojoUtil.copyClassFromTo(dto, ScoreDetailDTO.class);
			
			Integer nTotalSocre = dto.getTotalOrgScore();
			dto.setTotalOrgScore(nTotalSocre);
			
			scoreDetailList.add(dto);
		}
		
		return scoreDetailList;
	}
	
	// “积分排名”中登陆用户所在支部的排名
	@Override
	public Object findeMyRankByOrgScore(ScoreDetailDTO scoreDetailDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
//		List<String> orgIdList = new ArrayList<String>();
//		
//		orgIdList.add(scoreDetailDTO.getOrgId());
//		User userIn = new User();
//		userIn.setUserId(scoreDetailDTO.getUserId());
//		UserDTO userRet = userDao.selectUserInfoById(userIn);
		
		String subOrgId = scoreDetailDTO.getSubOrgId();
		
		List<ScoreDetailDTO> dtoList = new ArrayList<ScoreDetailDTO>();
		
		if (subOrgId != null && !subOrgId.isEmpty()) {
			dtoList = scoreDetailDAO.selectTopOrgScore(null, null);
		}
		
		if (dtoList != null && !dtoList.isEmpty()) {
			for (ScoreDetailDTO scoreDetail: dtoList) {
				nRank ++;
				if (scoreDetail.getSubOrgId() != null
						&& !scoreDetail.getSubOrgId().isEmpty()) {
					
					if (scoreDetail.getSubOrgId().equals(subOrgId)) {
						nMyRank = Integer.valueOf(nRank);
						break;
					}
				}
				
			}
		}
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyOrgRank(nMyRank);
		return userRet;
	}

	// 获取“支部排名”积分排名中当前组织下所有支部排名及登陆用户所在支部的排名
	@Override
	public Object findTopAndMyRankByOrgScore(ScoreDetailDTO scoreDetailDTO) {
		
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(scoreDetailDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		scoreDetailDTO.setOrgId(userDTO.getUserOrg());
		scoreDetailDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“支部排名”积分排名中当前组织下所有支部排名
		List<ScoreDetailDTO> scoreDetailDTOList = (List<ScoreDetailDTO>) findTopRankByOrgScore(scoreDetailDTO);
		
		// 获取“支部排名”积分排名中登陆用户所在支部的排名
		dto = (UserDTO) findeMyRankByOrgScore(scoreDetailDTO);
		dto.setScoreDetailList(scoreDetailDTOList);
		
		return dto;
	}

	// 分页查询学分 
	@Override
	public Object findScoreDetailList(ScoreDetailDTO queryDTO, Integer currentPage, Integer pageSize) {

		// 查询学分总数
		Integer nScoreDetailSum = scoreDetailDAO.findScoreDetailSum(queryDTO);
		
		ScoreQueryDTO scoreQueryDTO = new ScoreQueryDTO();
		scoreQueryDTO.setnScoreDetailSum(nScoreDetailSum);
		
		List<ScoreDetailDTO> dtoList = new ArrayList<ScoreDetailDTO>();
		
		PageCond pageCond = new PageCond(currentPage,pageSize);
		
		PageCond pageCondQuery = PageCondUtil.check(pageCond);
		
		List<ScoreDetailDTO> scoreDetailList = 
				scoreDetailDAO.findScoreDetailList(queryDTO, pageCondQuery);
		
		for (ScoreDetailDTO scoreDetail: scoreDetailList) {
			
//			ScoreDetailDTO dto = ClonePojoUtil.copyClassFromTo(scoreDetail, ScoreDetailDTO.class);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date dScoreDate = scoreDetail.getCreatTime();
			
			String strScoreDate = sdf.format(dScoreDate);
			scoreDetail.setStrScoreDate(strScoreDate);
			
			dtoList.add(scoreDetail);
		}
		
		Integer nScoreDetailCnt = scoreDetailDAO.findScoreDetailCnt(queryDTO);
		
		pageCondQuery.setTotalCount(nScoreDetailCnt);
		pageCondQuery.setPageCount(PageCondUtil.calculatePageCount(pageCondQuery));
		
		PageResult<ScoreDetailDTO> pageResult = new PageResult<ScoreDetailDTO>(dtoList, pageCondQuery);
		
		scoreQueryDTO.setPageResult(pageResult);
		
		return scoreQueryDTO;
	}

	@Override
	public Object findWxRegByOpenId(WxInfo wxInfo) {
		
		// 根据传入的code 查询用户openId
		WxLogin wxLogin = new WxLogin();
		// 根据code 获取accessToken\openId
		WxInfo wxDTO = null;
		WxUserInfo wxUserDTO = null;
		try {
			wxDTO = wxLogin.getTokenAndOpenId(wxInfo);
			// 根据openId 获取用户基本信息
			wxUserDTO = wxLogin.getUserInfo(wxDTO);
			
			UserDTO userDTO = userDao.findUserByOpenId(wxUserDTO.getOpenid());
			if (userDTO != null && userDTO.getUserId() != null) {
				// 已关联openId
				wxUserDTO.setUserWechatid(userDTO.getUserWechatid());// 微信号
				wxUserDTO.setRegStatus("1");
			} else {
				wxUserDTO.setRegStatus("0");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			log.error("获取微信信息失败！");
		}
		
		return wxUserDTO;
	}

	// 获取“成绩排名”中前几名
	@Override
	public Object findTopRankByExam(ExamScoreDTO examScoreDTO) {
		// 根据orgId 查询所有parentOrgId
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(examScoreDTO.getOrgId());
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ExamScoreDTO> dtoList = examScoreDao.selectTopUserExam(orgIdList, null, rankNum);
		
		List<ExamScoreDTO> examScoreList = new ArrayList<ExamScoreDTO>();
		
		for (ExamScoreDTO dto: dtoList) {
			
//			ExamScoreDTO examScore = ClonePojoUtil.copyClassFromTo(dto, ExamScoreDTO.class);
			
			Integer nTotalExamScore = dto.getTotalExamScore();
			dto.setTotalExamScore(nTotalExamScore);
			
			examScoreList.add(dto);
		}
		
		return examScoreList;
	}

	// "成绩排名"中登录用户的排名
	@Override
	public Object findeMyRankByExam(ExamScoreDTO examScoreDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(examScoreDTO.getOrgId());
		
		List<ExamScoreDTO> dtoList = examScoreDao.selectTopUserExam(orgIdList, null, null);
		
		for (ExamScoreDTO examScore: dtoList) {
			nRank ++;
			if (examScore.getUserId().equals(examScoreDTO.getUserId())) {
				nMyRank = Integer.valueOf(nRank);
				break;
			}
		}
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“成绩排名”中当前组织下所有用户排名及登陆用户的排名
	@Override
	public Object findTopAndMyRankByExam(ExamScoreDTO examScoreDTO) {
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		examScoreDTO.setOrgId(userDTO.getUserOrg());
		examScoreDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“成绩排名”中当前组织下所有用户排名
		List<ExamScoreDTO> examScoreList = (List<ExamScoreDTO>) findTopRankByExam(examScoreDTO);
		
		// 获取“成绩排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankByExam(examScoreDTO);
		dto.setExamScoreList(examScoreList);
		
		return dto;
	}
	
	// 获取“成绩排名”中前几名
	@Override
	public Object findTopRankByOrgExam(ExamScoreDTO examScoreDTO) {
		// 根据orgId 查询所有parentOrgId
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(examScoreDTO.getOrgId());
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ExamScoreDTO> dtoList = examScoreDao.selectTopOrgExam(null, rankNum);
		
		List<ExamScoreDTO> examScoreList = new ArrayList<ExamScoreDTO>();
		
		for (ExamScoreDTO dto: dtoList) {
			
//			ExamScoreDTO examScore = ClonePojoUtil.copyClassFromTo(dto, ExamScoreDTO.class);
			
			Integer nTotalExamScore = dto.getTotalOrgExamScore();
			dto.setTotalOrgExamScore(nTotalExamScore);
			
			examScoreList.add(dto);
		}
		
		return examScoreList;
	}

	// "支部排名"成绩排名中登录用户所在支部的排名
	@Override
	public Object findeMyRankByOrgExam(ExamScoreDTO examScoreDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		List<String> orgIdList = new ArrayList<String>();
		
		orgIdList.add(examScoreDTO.getOrgId());
		
		String subOrgId = examScoreDTO.getSubOrgId();
		
		List<ExamScoreDTO> dtoList = new ArrayList<ExamScoreDTO>();
		
		if (subOrgId != null && !subOrgId.isEmpty()) {
			dtoList = examScoreDao.selectTopOrgExam(null, null);
		}
		
		if (dtoList != null && !dtoList.isEmpty()) {
			for (ExamScoreDTO examScore: dtoList) {
				nRank ++;
				if (examScore.getSubOrgId().equals(subOrgId)) {
					nMyRank = Integer.valueOf(nRank);
					break;
				}
			}
		}
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyOrgRank(nMyRank);
		return userRet;
	}

	// 获取“支部排名”-成绩排名中当前组织下所有支部排名及登陆用户所在支部的排名
	@Override
	public Object findTopAndMyRankByOrgExam(ExamScoreDTO examScoreDTO) {
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
		examScoreDTO.setOrgId(userDTO.getUserOrg());
		examScoreDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“成绩排名”中当前组织下所有用户排名
		List<ExamScoreDTO> examScoreList = (List<ExamScoreDTO>) findTopRankByOrgExam(examScoreDTO);
		
		// 获取“成绩排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankByOrgExam(examScoreDTO);
		dto.setExamScoreList(examScoreList);
		
		return dto;
	}
	
	// 获取“支部排名”中支部中成绩排名中前几名
	@Override
	public Object findTopRankByExamInSubOrg(ExamScoreDTO examScoreDTO) {
		
		String subOrgId = examScoreDTO.getSubOrgId();
		
		// 取前十名
		Integer rankNum = Integer.valueOf(10);
		
		List<ExamScoreDTO> dtoList = examScoreDao.selectTopUserExam(null, subOrgId, rankNum);
		
		List<ExamScoreDTO> examScoreList = new ArrayList<ExamScoreDTO>();
		
		for (ExamScoreDTO dto: dtoList) {
			
//			ExamScoreDTO examScore = ClonePojoUtil.copyClassFromTo(dto, ExamScoreDTO.class);
			
			Integer nTotalExamScore = dto.getTotalExamScore();
			dto.setTotalExamScore(nTotalExamScore);
			
			examScoreList.add(dto);
		}
		
		return examScoreList;
	}

	// "成绩排名"中登录用户在支部中的排名
	@Override
	public Object findeMyRankByExamInSubOrg(ExamScoreDTO examScoreDTO) {
		Integer nMyRank = 0;
		int nRank = 0;
		
		String subOrgId = examScoreDTO.getSubOrgId();
		
		List<ExamScoreDTO> dtoList = examScoreDao.selectTopUserExam(null, subOrgId, null);
		
		for (ExamScoreDTO examScore: dtoList) {
			nRank ++;
			if (examScore.getUserId().equals(examScoreDTO.getUserId())) {
				nMyRank = Integer.valueOf(nRank);
				break;
			}
		}
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userRet = userDao.selectUserInfoById(user);
		
		userRet.setMyRank(nMyRank);
		return userRet;
	}

	// 获取“成绩排名”中当前组织下所有用户排名及登陆用户在支部中的排名
	@Override
	public Object findTopAndMyRankByExamInSubOrg(ExamScoreDTO examScoreDTO) {
		UserDTO dto = new UserDTO();
		
		User user = new User();
		user.setUserId(examScoreDTO.getUserId());
		UserDTO userDTO = userDao.selectUserInfoById(user);
//		examScoreDTO.setOrgId(userDTO.getUserOrg());
		examScoreDTO.setSubOrgId(userDTO.getUserSubOrg());
		
		//获取“成绩排名”中当前组织下所有用户排名
		List<ExamScoreDTO> examScoreList = (List<ExamScoreDTO>) findTopRankByExamInSubOrg(examScoreDTO);
		
		// 获取“成绩排名”中登陆用户的排名
		dto = (UserDTO) findeMyRankByExamInSubOrg(examScoreDTO);
		dto.setExamScoreList(examScoreList);
		
		return dto;
	}

}
